var express = require('express')
var router = express.Router()
var connection = require('../db/sql.js')

/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express' })
})

router.get('/getToken', function (req, res, next) {
  const userAccount = req.query.userAccount || req.body.userAccount

  //引入token包
  let jwt = require('jsonwebtoken')
  //用户信息
  let payload = { account: userAccount }
  //口令
  let secret = 'vmall'
  //生成token
  let token = jwt.sign(payload, secret)
  res.send({
    code: 200,
    data: {
      token,
    },
  })
})

//首页-分类数据
router.get('/api/category', function (req, res, next) {
  console.log('请求了category数据 :>> ')
  // res.header('Access-Control-Allow-Origin', 'http://localhost:5173') // 允许来自特定源的请求
  // res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
  res.send({
    code: 200,
    data: {
      categoryList: [
        { id: '001', title: '手机', imgUrl: 'images/1.webp' },
        { id: '002', title: '女鞋', imgUrl: 'images/2.webp' },
        { id: '003', title: '男装', imgUrl: 'images/3.webp' },
        { id: '004', title: '女装', imgUrl: 'images/4.webp' },
        { id: '005', title: '数码', imgUrl: 'images/5.webp' },
        { id: '006', title: '美食', imgUrl: 'images/6.webp' },
        { id: '007', title: '古玩', imgUrl: 'images/7.webp' },
        { id: '008', title: '童装', imgUrl: 'images/8.webp' },
        { id: '009', title: '母婴', imgUrl: 'images/9.webp' },
        { id: '010', title: '饮料', imgUrl: 'images/10.webp' },
      ],
    },
  })
})

//首页-商品数据
router.get('/api/goods', function (req, res, next) {
  console.log('goods :>> ')
  connection.query(
    'SELECT id,name,price,sales,img_url as imgUrl,actor,actor_name as actorName,actor_tag as actorTag,baoyou_url as baoyouUrl,left_url as leftUrl,right_url as rightUrl FROM vm_goods_list order by id',
    function (err, result) {
      if (err) {
        console.error('Database query error:', err)
        return result.status(500).send({
          code: 0,
          message: '服务器内部错误',
        })
      }
      res.send({
        code: 200,
        data: {
          goodsList: result,
        },
      })
    },
  )
})

router.get('/api/goods/searchList', function (req, res, next) {
  const searchName = req?.query?.keyword || ''
  connection.query(
    'SELECT * FROM vm_goods_list WHERE name LIKE ?',
    ['%' + searchName + '%'],
    function (error, results) {
      if (error) {
        console.error('Database query error:', error)
        return res.status(500).send({
          code: 0,
          message: '服务器内部错误',
        })
      }
      res.send({
        code: 1,
        data: results,
      })
    },
  )
})

//通过商品id查询
router.get('/api/goods/id', function (req, res, next) {
  console.log('/api/goods/id :>> ')
  //前端传给后端的id
  let id = req.query.id
  connection.query(
    'SELECT id,name,images,price,sales,img_url as imgUrl,actor,actor_name as actorName,actor_tag as actorTag,baoyou_url as baoyouUrl,left_url as leftUrl,right_url as rightUrl FROM vm_goods_list where id=?',
    [id],
    function (error, result) {
      console.log('result-object :>> ', result)
      if (error) {
        console.error('Database query error:', error)
        return result.status(500).send({
          code: 0,
          message: '服务器内部错误',
        })
      }
      res.json({
        code: 200,
        data: result[0],
      })
    },
  )
})

module.exports = router
